<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { reactive, watch, inject, Ref, onBeforeUnmount } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { Row } from '@/api/warehouse'
const { required } = useValidator()
const currentRow: Ref<Row> = inject('currentRow') as Ref<Row>
const { formRegister, formMethods } = useForm()

const { setValues, getFormData, getElFormExpose } = formMethods

const schema = reactive<FormSchema[]>([
  {
    field: '',
    label: '基础信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'name',
    label: '仓库名称',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 50, message: '最多输入50个字符', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请输入(最多50个字符)'
    }
  },
  {
    field: 'real_name',
    label: '负责人',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 50, message: '请输入最多支持50字符', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请输入(最多50个字符)'
    }
  },
  {
    field: 'mobile',
    label: '联系方式',
    component: 'Input',
    formItemProps: {
      rules: [
        required(),
        { min: 11, max: 11, message: '请输入负责人联系方式', trigger: 'blur' },
        {
          pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
          message: '请输入负责人联系方式',
          trigger: 'blur'
        }
      ]
    }
  },
  {
    field: 'address',
    label: '仓库地址',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 100, message: '最多输入100个字符', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请输入(最多100个字符)'
    }
  },
  {
    field: 'acreage',
    label: '仓库面积m²',
    component: 'Input',
    formItemProps: {
      rules: [{ min: 0.01, message: '请输入', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请输入(最小支持小数点后两位)'
    }
  },
  {
    field: 'long',
    label: '仓库经度',
    component: 'Input',
    formItemProps: {
      rules: [
        {
          pattern:
            /^-?((0|[1-9]\d?|([1-9]\d(\.\d{1,6})?))|([1-9]\d{0,2}(\.\d{1,6})?)|([1-8]\d{0,2}(\.\d{1,6})?)|(90(\.0{1,6})?))$/,
          message: '请输入有效的经度',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      placeholder: '请输入经度'
    }
  },
  {
    field: 'lat',
    label: '仓库纬度',
    component: 'Input',
    formItemProps: {
      rules: [
        {
          pattern:
            /^-?((0|[1-9]\d?|([1-9]\d(\.\d{1,6})?))|([1-9]\d{0,2}(\.\d{1,6})?)|([1-8]\d{0,2}(\.\d{1,6})?)|(90(\.0{1,6})?))$/,
          message: '请输入有效的纬度',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      placeholder: '请输入纬度'
    }
  }
])

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    const formDataList = {
      id: formData.id,
      address: formData.address,
      mobile: formData.mobile,
      name: formData.name,
      acreage: formData.acreage,
      real_name: formData.real_name,
      long: formData.long,
      lat: formData.lat
    }
    return formDataList
  }
}

const reset = async () => {
  const elForm = await getElFormExpose()
  elForm?.resetFields()
}

watch(
  () => currentRow.value,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

onBeforeUnmount(reset)

defineExpose({
  submit,
  reset
})
</script>

<template>
  <Form @register="formRegister" :schema="schema" />
</template>
